home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / cmu-snmp1.0 / apps / snmpnetstat / route.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-20  |  11.6 KB  |  434 lines

  1. /***********************************************************
  2.     Copyright 1989 by Carnegie Mellon University
  3.  
  4.                       All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute this software and its 
  7. documentation for any purpose and without fee is hereby granted, 
  8. provided that the above copyright notice appear in all copies and that
  9. both that copyright notice and this permission notice appear in 
  10. supporting documentation, and that the name of CMU not be
  11. used in advertising or publicity pertaining to distribution of the
  12. software without specific, written prior permission.  
  13.  
  14. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  15. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  16. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  17. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  18. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  19. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  20. SOFTWARE.
  21. ******************************************************************/
  22. /*
  23.  * Copyright (c) 1983,1988 Regents of the University of California.
  24.  * All rights reserved.
  25.  *
  26.  * Redistribution and use in source and binary forms are permitted
  27.  * provided that this notice is preserved and that due credit is given
  28.  * to the University of California at Berkeley. The name of the University
  29.  * may not be used to endorse or promote products derived from this
  30.  * software without specific prior written permission. This software
  31.  * is provided ``as is'' without express or implied warranty.
  32.  */
  33.  
  34. #include <stdio.h>
  35. #include <strings.h>
  36. #include <ctype.h>
  37.  
  38. #include <sys/param.h>
  39. #include <sys/socket.h>
  40.  
  41. #include <netinet/in.h>
  42. #define    LOOPBACKNET 127
  43.  
  44. #include <netdb.h>
  45. #include "main.h"
  46. #include "asn1.h"
  47. #include "snmp.h"
  48. #include "snmp_impl.h"
  49. #include "snmp_api.h"
  50. #include "snmp_client.h"
  51. #include "mib.h"
  52.  
  53. extern    int nflag;
  54. extern    char *routename(), *netname(), *plural();
  55. extern    char *malloc();
  56. extern    struct snmp_session *Session;
  57. extern    struct variable_list *getvarbyname();
  58. extern    int print_errors;
  59.  
  60.  
  61. struct route_entry {
  62.     oid        instance[4];
  63.     struct in_addr  destination;
  64.     int        set_destination;
  65.     struct in_addr  gateway;
  66.     int        set_gateway;
  67.     int        interface;
  68.     int        set_interface;
  69.     int        type;
  70.     int        set_type;
  71.     int        proto;
  72.     int        set_proto;
  73.     char    ifname[64];
  74.     int        set_name;
  75. };
  76.  
  77.  
  78.  
  79. #define RTDEST        1
  80. #define RTIFINDEX   2
  81. #define RTNEXTHOP   7
  82. #define RTTYPE        8
  83. #define RTPROTO        9
  84. static oid oid_rttable[] = {1, 3, 6, 1, 2, 1, 4, 21, 1};
  85. static oid oid_rtdest[] = {1, 3, 6, 1, 2, 1, 4, 21, 1, 1};
  86. static oid oid_rtifindex[] = {1, 3, 6, 1, 2, 1, 4, 21, 1, 2};
  87. static oid oid_rtnexthop[] = {1, 3, 6, 1, 2, 1, 4, 21, 1, 7};
  88. static oid oid_rttype[] = {1, 3, 6, 1, 2, 1, 4, 21, 1, 8};
  89. static oid oid_rtproto[] = {1, 3, 6, 1, 2, 1, 4, 21, 1, 9};
  90. static oid oid_ifdescr[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 2};
  91. static oid oid_ipnoroutes[] = {1, 3, 6, 1, 2, 1, 4, 12, 0};
  92.  
  93.  
  94. /*
  95.  * Print routing tables.
  96.  */
  97. routepr()
  98. {
  99.     struct route_entry route, *rp = &route;
  100.     struct snmp_pdu *request, *response;
  101.     struct variable_list *vp;
  102.     char name[16], *flags;
  103.     oid *instance, type;
  104.     int toloopback, status;
  105.     char ch;
  106.  
  107.     printf("Routing tables\n");
  108.     printf("%-16.16s %-18.18s %-6.6s  %s\n",
  109.         "Destination", "Gateway",
  110.         "Flags", "Interface");
  111.  
  112.  
  113.     request = snmp_pdu_create(GETNEXT_REQ_MSG);
  114.  
  115.     snmp_add_null_var(request, oid_rtdest, sizeof(oid_rtdest)/sizeof(oid));
  116.     snmp_add_null_var(request, oid_rtifindex, sizeof(oid_rtifindex)/sizeof(oid));
  117.     snmp_add_null_var(request, oid_rtnexthop, sizeof(oid_rtnexthop)/sizeof(oid));
  118.     snmp_add_null_var(request, oid_rttype, sizeof(oid_rttype)/sizeof(oid));
  119.     snmp_add_null_var(request, oid_rtproto, sizeof(oid_rtproto)/sizeof(oid));
  120.  
  121.     while(request){
  122.         status = snmp_synch_response(Session, request, &response);
  123.         if (status != STAT_SUCCESS || response->errstat != SNMP_ERR_NOERROR){
  124.         fprintf(stderr, "SNMP request failed\n");
  125.         break;
  126.         }
  127.         instance = NULL;
  128.         request = NULL;
  129.         rp->set_destination = 0;
  130.         rp->set_interface = 0;
  131.         rp->set_gateway = 0;
  132.         rp->set_type = 0;
  133.         rp->set_proto = 0;
  134.         for(vp = response->variables; vp; vp = vp->next_variable){
  135.         if (vp->name_length != 14 ||
  136.             bcmp((char *)vp->name, (char *)oid_rttable, sizeof(oid_rttable))){
  137.             continue;    /* if it isn't in this subtree, just continue */
  138.         }
  139.  
  140.         if (instance != NULL){
  141.             oid *ip, *op;
  142.             int count;
  143.  
  144.             ip = instance;
  145.             op = vp->name + 10;
  146.             for(count = 0; count < 4; count++){
  147.             if (*ip++ != *op++)
  148.                 break;
  149.             }
  150.             if (count < 4)
  151.             continue;    /* not the right instance, ignore */
  152.         } else {
  153.             instance = vp->name + 10;
  154.         }
  155.         /*
  156.          * At this point, this variable is known to be in the routing table
  157.          * subtree, and is of the right instance for this transaction.
  158.          */
  159.  
  160.         if (request == NULL)
  161.             request = snmp_pdu_create(GETNEXT_REQ_MSG);
  162.         snmp_add_null_var(request, vp->name, vp->name_length);
  163.  
  164.         type = vp->name[9];
  165.         switch ((char)type){
  166.             case RTDEST:
  167.             bcopy((char *)vp->val.string, (char *)&rp->destination, sizeof(u_long));
  168.             rp->set_destination = 1;
  169.             break;
  170.             case RTIFINDEX:
  171.             rp->interface = *vp->val.integer;
  172.             rp->set_interface = 1;
  173.             break;
  174.             case RTNEXTHOP:
  175.             bcopy((char *)vp->val.string, (char *)&rp->gateway, sizeof(u_long));
  176.             rp->set_gateway = 1;
  177.             break;
  178.             case RTTYPE:
  179.             rp->type = *vp->val.integer;
  180.             rp->set_type = 1;
  181.             break;
  182.             case RTPROTO:
  183.             rp->proto = *vp->val.integer;
  184.             rp->set_proto = 1;
  185.             break;
  186.         }
  187.         }
  188.         if (!(rp->set_destination && rp->set_gateway
  189.         && rp->set_type && rp->set_interface)){
  190.             snmp_free_pdu(request);
  191.             request = 0;
  192.             continue;
  193.         }
  194.         toloopback = *(char *)&rp->gateway == LOOPBACKNET;
  195.         printf("%-16.16s ",
  196.         (rp->destination.s_addr == 0) ? "default" :
  197.         (toloopback) ?
  198.         routename(rp->destination) : netname(rp->destination, 0L));
  199.         printf("%-18.18s ", routename(rp->gateway));
  200.         flags = name;
  201.         *flags++ = 'U'; /* route is in use */
  202.         /* this !toloopback shouldnt be necessary */
  203.         if (!toloopback && rp->type == MIB_IPROUTETYPE_REMOTE)
  204.         *flags++ = 'G';
  205.         if (toloopback)
  206.         *flags++ = 'H';
  207.         if (rp->proto == MIB_IPROUTEPROTO_ICMP)
  208.         *flags++ = 'D';    /* redirect */
  209.         *flags = '\0';
  210.         printf("%-6.6s ", name);
  211.         get_ifname(rp->ifname, rp->interface);
  212.         ch = rp->ifname[strlen(rp->ifname) - 1];
  213.         ch = '5';   /* force the if statement */
  214.         if (isdigit(ch))
  215.         printf(" %.32s\n", rp->ifname);
  216.         else
  217.         printf(" %.32s%d\n", rp->ifname, rp->interface);
  218.  
  219.     }
  220. }
  221.  
  222. struct iflist {
  223.     int    index;
  224.     char name[64];
  225.     struct iflist *next;
  226. } *Iflist = NULL;
  227.  
  228. get_ifname(name, index)
  229.     char *name;
  230.     int index;
  231. {
  232.     struct snmp_pdu *pdu, *response;
  233.     struct variable_list *vp;
  234.     struct iflist *ip;
  235.     oid varname[32];
  236.     int status;
  237.  
  238.     for(ip = Iflist; ip; ip = ip->next){
  239.     if (ip->index == index)
  240.         break;
  241.     }
  242.     if (ip){
  243.     strcpy(name, ip->name);
  244.     return;
  245.     }
  246.     ip = (struct iflist *)malloc(sizeof(struct iflist));
  247.     ip->next = Iflist;
  248.     Iflist = ip;
  249.     ip->index = index;
  250.     pdu = snmp_pdu_create(GET_REQ_MSG);
  251.     bcopy((char *)oid_ifdescr, (char *)varname, sizeof(oid_ifdescr));
  252.     varname[10] = (oid)index;
  253.     snmp_add_null_var(pdu, varname, sizeof(oid_ifdescr)/sizeof(oid) + 1);
  254.     status = snmp_synch_response(Session, pdu, &response);
  255.     if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR){
  256.     vp = response->variables;
  257.     bcopy((char *)vp->val.string, ip->name, vp->val_len);
  258.     ip->name[vp->val_len] = '\0';
  259.     } else {
  260.     sprintf(ip->name, "if%d", index);
  261.     }    
  262.     strcpy(name, ip->name);
  263. }
  264.  
  265. char *
  266. routename(in)
  267.     struct in_addr in;
  268. {
  269.     register char *cp;
  270.     static char line[MAXHOSTNAMELEN + 1];
  271.     struct hostent *hp;
  272.     static char domain[MAXHOSTNAMELEN + 1];
  273.     static int first = 1;
  274.     char *index();
  275.  
  276.     if (first) {
  277.         first = 0;
  278.         if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
  279.             (cp = index(domain, '.')))
  280.             (void) strcpy(domain, cp + 1);
  281.         else
  282.             domain[0] = 0;
  283.     }
  284.     cp = 0;
  285.     if (!nflag) {
  286.         hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
  287.             AF_INET);
  288.         if (hp) {
  289.             if ((cp = index(hp->h_name, '.')) &&
  290.                 !strcmp(cp + 1, domain))
  291.                 *cp = 0;
  292.             cp = hp->h_name;
  293.         }
  294.     }
  295.     if (cp)
  296.         strncpy(line, cp, sizeof(line) - 1);
  297.     else {
  298. #define C(x)    ((x) & 0xff)
  299.         in.s_addr = ntohl(in.s_addr);
  300.         sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
  301.             C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
  302.     }
  303.     return (line);
  304. }
  305.  
  306. /*
  307.  * Return the name of the network whose address is given.
  308.  * The address is assumed to be that of a net or subnet, not a host.
  309.  */
  310. char *
  311. netname(in, mask)
  312.     struct in_addr in;
  313.     u_long mask;
  314. {
  315.     char *cp = 0;
  316.     static char line[MAXHOSTNAMELEN + 1];
  317.     struct netent *np = 0;
  318.     u_long net;
  319.     register i;
  320.     int subnetshift;
  321.  
  322.     i = ntohl(in.s_addr);
  323.     if (!nflag && i) {
  324.         if (mask == 0) {
  325.             if (IN_CLASSA(i)) {
  326.                 mask = IN_CLASSA_NET;
  327.                 subnetshift = 8;
  328.             } else if (IN_CLASSB(i)) {
  329.                 mask = IN_CLASSB_NET;
  330.                 subnetshift = 8;
  331.             } else {
  332.                 mask = IN_CLASSC_NET;
  333.                 subnetshift = 4;
  334.             }
  335.             /*
  336.              * If there are more bits than the standard mask
  337.              * would suggest, subnets must be in use.
  338.              * Guess at the subnet mask, assuming reasonable
  339.              * width subnet fields.
  340.              */
  341.             while (i &~ mask)
  342.                 mask = (long)mask >> subnetshift;
  343.         }
  344.         net = i & mask;
  345.         while ((mask & 1) == 0)
  346.             mask >>= 1, net >>= 1;
  347.         np = getnetbyaddr(net, AF_INET);
  348.         if (np)
  349.             cp = np->n_name;
  350.     }    
  351.     if (cp)
  352.         strncpy(line, cp, sizeof(line) - 1);
  353.     else if ((i & 0xffffff) == 0)
  354.         sprintf(line, "%u", C(i >> 24));
  355.     else if ((i & 0xffff) == 0)
  356.         sprintf(line, "%u.%u", C(i >> 24) , C(i >> 16));
  357.     else if ((i & 0xff) == 0)
  358.         sprintf(line, "%u.%u.%u", C(i >> 24), C(i >> 16), C(i >> 8));
  359.     else
  360.         sprintf(line, "%u.%u.%u.%u", C(i >> 24),
  361.             C(i >> 16), C(i >> 8), C(i));
  362.     return (line);
  363. }
  364.  
  365. /*
  366.  * Print routing statistics
  367.  */
  368. rt_stats()
  369. {
  370.     struct variable_list *var;
  371.  
  372.     printf("routing:\n");
  373.     var = getvarbyname(Session, oid_ipnoroutes, sizeof(oid_ipnoroutes) / sizeof(oid));
  374.     if (var){
  375.         printf("\t%u destination%s found unreachable\n",
  376.         *var->val.integer, plural((int)*var->val.integer));
  377.     } else {
  378.         printf("\tCouldn't get ipOutNoRoutes variable\n");
  379.     }
  380. }
  381.  
  382. /*
  383.  * Request a variable with a GET REQUEST message on the given
  384.  * session.  The session must have been opened as a synchronous
  385.  * session (synch_setup_session()).  If the variable is found, a
  386.  * pointer to a struct variable_list object will be returned.
  387.  * Otherwise, NULL is returned.  The caller must free the returned
  388.  * variable_list object when done with it.
  389.  */
  390. struct variable_list *
  391. getvarbyname(sp, name, len)
  392.     struct snmp_session *sp;
  393.     oid    *name;
  394.     int len;
  395. {
  396.     struct snmp_pdu *request, *response;
  397.     struct variable_list *var = NULL, *vp;
  398.     int status;
  399.  
  400.     request = snmp_pdu_create(GET_REQ_MSG);
  401.  
  402.     snmp_add_null_var(request, name, len);
  403.  
  404.     status = snmp_synch_response(sp, request, &response);
  405.  
  406.     if (status == STAT_SUCCESS){
  407.     if (response->errstat == SNMP_ERR_NOERROR){
  408.         for(var = response->variables; var; var = var->next_variable){
  409.         if (var->name_length == len && !bcmp(name, var->name, len * sizeof(oid)))
  410.             break;    /* found our match */
  411.         }
  412.         if (var != NULL){
  413.         /*
  414.          * Now unlink this var from pdu chain so it doesn't get freed.
  415.          * The caller will free the var.
  416.          */
  417.         if (response->variables == var){
  418.             response->variables = var->next_variable;
  419.         } else {
  420.             for(vp = response->variables; vp; vp = vp->next_variable){
  421.             if (vp->next_variable == var){
  422.                 vp->next_variable = var->next_variable;
  423.                 break;
  424.             }
  425.             }
  426.         }
  427.         }
  428.     }
  429.     }
  430.     if (response)
  431.     snmp_free_pdu(response);
  432.     return var;
  433. }
  434.